// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <string>

#include <pollux/core/plan_node.h>
#include <pollux/testing/exec/result_verifier.h>
#include <pollux/testing/exec/util/assert_query_builder.h>
#include <pollux/plan/plan_builder.h>

namespace kumo::pollux::exec::test {

// The result of the avg(interval) -> row(double, bigint) -> interval function
// may have a precision error depending on the ordering of inputs due to the
// accumulation in the double intermediate state. This verifier convert the
// interval aggregaiton result to double before comparing the actual and
// expected results, so that the epsilon comparison for floating-point types in
// assertEqualResults() can kick in.
class AverageResultVerifier : public ResultVerifier {
 public:
  bool supportsCompare() override {
    return true;
  }

  bool supportsVerify() override {
    return false;
  }

  void initialize(
      const std::vector<RowVectorPtr>& /*input*/,
      const std::vector<std::string>& groupingKeys,
      const core::AggregationNode::Aggregate& aggregate,
      const std::string& aggregateName) override {
    if (aggregate.call->type()->isIntervalDayTime()) {
      projections_ = groupingKeys;
      projections_.push_back(
          fmt::format("cast(to_milliseconds({}) as double)", aggregateName));
    }
  }

  void initializeWindow(
      const std::vector<RowVectorPtr>& input,
      const std::vector<std::string>& /*partitionByKeys*/,
      const std::vector<SortingKeyAndOrder>& /*sortingKeysAndOrders*/,
      const core::WindowNode::Function& function,
      const std::string& /*frame*/,
      const std::string& windowName) override {
    if (function.functionCall->type()->isIntervalDayTime()) {
      projections_ = as_row_type(input[0]->type())->names();
      projections_.push_back(
          fmt::format("cast(to_milliseconds({}) as double)", windowName));
    }
  }

  bool compare(const RowVectorPtr& result, const RowVectorPtr& altResult)
      override {
    if (projections_.empty()) {
      return assertEqualResults({result}, {altResult});
    } else {
      return assertEqualResults({transform(result)}, {transform(altResult)});
    }
  }

  bool verify(const RowVectorPtr& /*result*/) override {
    POLLUX_UNSUPPORTED();
  }

  void reset() override {
    projections_.clear();
  }

 private:
  RowVectorPtr transform(const RowVectorPtr& data) {
    auto plan = PlanBuilder().values({data}).project(projections_).planNode();
    return AssertQueryBuilder(plan).copyResults(data->pool());
  }

  std::vector<std::string> projections_;
};

} // namespace kumo::pollux::exec::test
